﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HIDTOOL;
using System.IO;
using System.Runtime.InteropServices;
using System.Globalization;

namespace HIDTOOL
{
   //****************总的设置选项 结构体类型***********************本版本号 = 1

    public struct SettingMy
    {
        public UInt32 SettFileVer;          //设置文件版本号
        public bool NextlineEveryFrame;
        public bool HexShow;
        public bool HexSend;
        public UInt32 ClearAtNFrame;        //N帧后清除显示框
        public string SendContent;          //发送框的内容
        public UInt32 ShowNByte;            //仅显示一帧前N字节
        public string HID_Dir;              //HID完整路径
    };
    
   
    public partial class frm_Setting : Form
    {
        private mianForm MFOM;          //承接主窗口的控件
        public SettingMy MySetting;        //定义设置的结构体变量
        private readonly UInt32 CurrSettFileVer = 4;
        public bool NeedSaveSetting = false;
 
        IniFileOperate IniFile;
        public void CreateDefaultSetfile()
        { 
            MySetting.SettFileVer = CurrSettFileVer;
            MySetting.NextlineEveryFrame = chk_RxNewline.Checked = true;    //设置默认值
            MySetting.ClearAtNFrame = 0;
            MySetting.HexSend = MySetting.HexShow = true;               
            txt_ClearAtNFrame.Text = MySetting.ClearAtNFrame.ToString();
            MySetting.ShowNByte = 64;
            MySetting.HID_Dir = "";
            SettingFile_Write();
        }
        public frm_Setting(mianForm form)
        {
            InitializeComponent();

            IniFile = new IniFileOperate("HIDtool.ini");

            this.MFOM = form;
            
            if (IniFile.ExistINIFile() != true)     //设置文件不存在，则新建一个，写入默认值
            {
                CreateDefaultSetfile();
            }
            else                                    //文件存在读取文件的设置数据
            {
                SettingFile_Read();                
            }
        }

       
        //************读取设置*****************************************
        private bool SettingFile_Read()
        {
            if (IniFile.Read("Setting", "NextlineEveryFrame") == "true")
                MySetting.NextlineEveryFrame = true;
            else
                MySetting.NextlineEveryFrame = false;

            if (IniFile.Read("Setting", "HexShow") == "true")
                MySetting.HexShow = true;
            else
                MySetting.HexShow = false;

            if (IniFile.Read("Setting", "HexSend") == "true")
                MySetting.HexSend = true;
            else
                MySetting.HexSend = false;
            
            MFOM.txt_Send.Text = IniFile.Read("Setting", "SendContent");

            try
            {
                
                MySetting.SettFileVer = Convert.ToUInt16(IniFile.Read("SettFileVer", "CurrVer"), 10);
                MySetting.ClearAtNFrame = Convert.ToUInt16(IniFile.Read("Setting", "ClearAtNFrame"), 10);                
                MySetting.ShowNByte = Convert.ToUInt16(IniFile.Read("Setting", "ShowNByte"), 10);
            }
            catch
            {
                MessageBox.Show("设置文件出错，软件设置可能不正常，您需要重新设置", "出错");
                IniFile.DelIniFile();
                CreateDefaultSetfile();
            }

            if (MySetting.SettFileVer >= CurrSettFileVer)           //设置文件版本，比本软件规定的版本高，正常读取
            { 
                MFOM.txt_ShowRX.BackColor = System.Drawing.ColorTranslator.FromHtml(IniFile.Read("Setting", "RX_BackColor"));
                MFOM.txt_ShowRX.ForeColor = System.Drawing.ColorTranslator.FromHtml(IniFile.Read("Setting", "RX_FontColor"));
            
                FontConverter fc = new FontConverter();
                MFOM.txt_ShowRX.Font = (Font)fc.ConvertFromString(IniFile.Read("Setting", "RX_Font"));

                MySetting.HID_Dir = IniFile.Read("HIDstting", "HID_Dir");
            }

            return true;
        }

        //************写入设置*****************************************
        public bool SettingFile_Write()
        {
            IniFile.Write("SettFileVer", "CurrVer", CurrSettFileVer.ToString());

            if (MySetting.NextlineEveryFrame)
                IniFile.Write("Setting", "NextlineEveryFrame", "true");
            else
                IniFile.Write("Setting", "NextlineEveryFrame", "false");

            if (MySetting.HexShow)
                IniFile.Write("Setting", "HexShow", "true");
            else
                IniFile.Write("Setting", "HexShow", "false");

            if (MySetting.HexSend)
                IniFile.Write("Setting", "HexSend", "true");
            else
                IniFile.Write("Setting", "HexSend", "false");

            IniFile.Write("Setting", "ClearAtNFrame", MySetting.ClearAtNFrame.ToString());
            IniFile.Write("Setting", "ShowNByte", MySetting.ShowNByte.ToString());
            IniFile.Write("Setting", "SendContent", MFOM.txt_Send.Text);
            IniFile.Write("HIDstting", "HID_Dir", MFOM.HID_Dir);

            FontConverter fc = new FontConverter();
            IniFile.Write("Setting", "RX_Font", fc.ConvertToInvariantString(MFOM.txt_ShowRX.Font));
            IniFile.Write("Setting", "RX_FontColor", System.Drawing.ColorTranslator.ToHtml(MFOM.txt_ShowRX.ForeColor));
            IniFile.Write("Setting", "RX_BackColor", System.Drawing.ColorTranslator.ToHtml(MFOM.txt_ShowRX.BackColor));
            return true;
        }
        private void frm_Setting_Load(object sender, EventArgs e)                       //窗口加载显示时候，更新控件
        {
            chk_RxNewline.Checked = MySetting.NextlineEveryFrame;
            txt_ClearAtNFrame.Text = MySetting.ClearAtNFrame.ToString();
            txt_ShowNByte.Text = MySetting.ShowNByte.ToString();
        }

        private void frm_Setting_FormClosing(object sender, FormClosingEventArgs e)     //窗口被关闭时候，更新设置文件
        {
            if (NeedSaveSetting == true)        //如果需要保存设置
            {
                if (txt_ClearAtNFrame.Text == "")
                    txt_ClearAtNFrame.Text = "0";
                if (txt_ShowNByte.Text == "")
                    txt_ShowNByte.Text = "0";
                MySetting.ClearAtNFrame = Convert.ToUInt16(txt_ClearAtNFrame.Text);
                MySetting.ShowNByte = Convert.ToUInt16(txt_ShowNByte.Text);
                if (MySetting.ShowNByte > 64)
                    MySetting.ShowNByte = 64;
                SettingFile_Write();
                NeedSaveSetting = false;     //需要保存改变
            }
        }
        private void txt_TxtBoxNumOnly_KeyPress(object sender, KeyPressEventArgs e)         //输入框仅限数字输入
        {
            NeedSaveSetting = true;     //需要保存改变
            if (HexDecProcess.InputIsDec(e) == false) //输入非数字 
                e.Handled = true;       //执行这一句，输入被取消   
            if (e.KeyChar == 0x0d)// or  回车
                this.Close();            
        }

        private void chk_RxNewline_CheckedChanged(object sender, EventArgs e)       //每帧换行状态改变
        {
            MySetting.NextlineEveryFrame = chk_RxNewline.Checked;
            NeedSaveSetting = true;     //需要保存改变
        }
    }
}































/*
 

 
 
 
 
 
 */